package com.batchbrake.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HandbrakeCLI {
	public static String PATH = "HandBrakeCLI";
	
	public static Map<String, List<String>> getPresets() throws IOException {
		String cmd[] = new String[] {
			PATH, "--preset-list"
		};
		
		Map<String, List<String>> presets = new HashMap<String, List<String>>();
		Process p = Runtime.getRuntime().exec(cmd);
		BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line = null;
		while( (line = r.readLine()) != null ) {
			if( line.startsWith("< ") ) {
				String key = line.substring(2);
				List<String> values = new ArrayList<String>();
				
				while( ((line = r.readLine()) != null) && !line.startsWith(">") ) {
					if( line.startsWith("   + ") ) {
						int end = line.indexOf(":");
						String preset = line.substring(5, end);
						values.add(preset);
					}
				}
				presets.put(key, values);
			}
		}
		return presets;
	}
}
